package test.my.com.myapplicationtest.bath_way_side_bar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.TextureView;
import android.view.View;

import test.my.com.myapplicationtest.R;


/**
 * Created by ThinkPad on 2015/5/9.
 */
public class BothWaySildeBar extends View implements TextureView.SurfaceTextureListener {

    private SildeBarConfigBuilder mSildeBarConfigBuilder;
    // private int mMinTextY, mMaxTextY;
    private int mMinTextHeight, mMaxTextHeight; // 文字的高度
    private Bitmap mLeftCentreIcon, mRightCenreIcon, mBottomLeftIcon, mBottomRightIcon;
    private boolean mCheckTag = true;
    private Canvas canvas;
    private boolean isSurface, isLeftSilde = true, isRightSlide = true;
    private int mTouchX, mTouchY, mTouchDownX, mTouchDownY;
    private Rect mLeftRange, mRightRange; // 左边&右边滑动块的范围大小
    private int orginal;
    private static final int SELECTMIN = 1;//选中最小值。
    private static final int SELECTMAX = 2;//选中最大值。
    private static final int SELECTAll = 3;//选中两个。
    private String mMinText, mMaxText;
    private static final int SILDEOFFSET = 20;//触摸滑动扩大值。
    private int mCurrentMin, mCurrentMax;
    private int mLeftMoveX, mRightMoveX; // 左&右X移动的距离
    private int mMinWidth, mMaxWidth; // 文字+中间图片+圆点的最大宽度
    private SildeTouchUpObserver mSildeTouchUpObserver;
    private boolean isShow;

    public BothWaySildeBar(Context context) {
        super(context);
        initialize();
    }

    public BothWaySildeBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    private void initialize() {
        mSildeBarConfigBuilder = null;
        //setSurfaceTextureListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas = canvas;
        if (TextUtils.isEmpty(mMinText) && mSildeBarConfigBuilder != null) {
            getLeftMaxWidth();
            getRightMaxWidth();
            startDraw();
        }
        if (mSildeBarConfigBuilder != null) {
            drawVarious(null);
        }
    }

    /**
     * 控件还原到初始状态。
     */
    public void rest() {
        mLeftMoveX = mMinWidth / 2;
        mRightMoveX = getWidth() - mMaxWidth / 2;
        mLeftRange = new Rect(0, 0, mMinWidth, getHeight());
        mRightRange = new Rect(getWidth() - mMaxWidth, 0, getWidth(), getHeight());
        mMinText = mSildeBarConfigBuilder.getContent().getMinValue()
                + mSildeBarConfigBuilder.getContent().getAdditionalText();
        mMaxText = mSildeBarConfigBuilder.getContent().getMaxValue()
                + mSildeBarConfigBuilder.getContent().getAdditionalRightText();
        mCurrentMin = mSildeBarConfigBuilder.getContent().getMinValue();
        mCurrentMax = mSildeBarConfigBuilder.getContent().getMaxValue();
        invalidate();
        //drawVarious(null);
    }

    /**
     * 年限定制。
     */
    public void performForYear(int min, int max, SildeTouchUpObserver sildeTouchUpObserver) {
//        int secondSpc = getResources().getDimensionPixelOffset(R.dimen.height2);
//        int bottomlineheight = getResources().getDimensionPixelOffset(R.dimen.height2);
//        int textsize = getResources().getDimensionPixelOffset(R.dimen.width14);
//        int conterPicWidth = getResources().getDimensionPixelOffset(R.dimen.width17);
//        int conterPicHeight = getResources().getDimensionPixelOffset(R.dimen.height27);
//        int bottomLinewh = getResources().getDimensionPixelOffset(R.dimen.width15);
        int secondSpc = 2;
        int bottomlineheight = 2;
        int textsize = 14;
        int conterPicWidth = 17;
        int conterPicHeight = 27;
        int bottomLinewh = 15;
        perform(min, max,
                R.drawable.icon_bothwaysildebar_drop1, R.drawable.icon_bothwaysildebar_drop2,
                R.drawable.icon_bothwaysildebar_round, R.drawable.icon_bothwaysildebar_round,
                0, secondSpc,
                Color.parseColor("#1081e0"), bottomlineheight,
                Color.parseColor("#666666"), textsize,
                conterPicWidth, conterPicHeight,
                bottomLinewh, bottomLinewh,
                "", "",
                sildeTouchUpObserver);
    }


    /**
     * 开始绘图。
     *
     * @param min         最小值。1
     * @param max         最大值。2
     * @param centrePicId 中间滑动图片的资源id。3
     * @param bottomPicId 底部滑动图片的资源id。4
     * @param spacing     文字和中间图片的间距。5 10
     * @param spacingSec  中间图片和底部图片的距离。6 4
     * @param bLineC      底部滑动线的颜色。7
     * @param bLineH      底部滑动线的高度。8 4
     * @param topTextC    顶部文字的颜色。9
     * @param topTextS    顶部文字的字号。10 28
     * @param cenPicW     中间图片的宽度。11 34
     * @param cenPicH     中间图片的高度。12 54
     * @param bPicW       底部图片的宽度。13 30
     * @param bPicH       底部图片的高度。14 30
     */
    public void perform(int min, int max,
                        int centrePicId, int centreRightPicId, int bottomPicId, int bottomRightPicId,
                        int spacing, int spacingSec,
                        int bLineC, int bLineH,
                        int topTextC, int topTextS,
                        int cenPicW, int cenPicH,
                        int bPicW, int bPicH,
                        String attach, String attachRight,
                        SildeTouchUpObserver observer) {
        setTouchObserver(observer);
        SildeBarContentModel model = new SildeBarContentModel();
        model.setMinValue(min);
        model.setMaxValue(max);
        model.setMinCentreIconResourceId(centrePicId);
        model.setMinBottomIconResourceId(bottomPicId);
        model.setMaxCentreIconResourceId(centreRightPicId);
        model.setMaxBottomIconResourceId(bottomRightPicId);
        model.setAdditionalText(attach);
        model.setAdditionalRightText(attachRight);
        SildeBarAttributeModel attribute = new SildeBarAttributeModel();
        attribute.setLeftSpacing(spacing);
        attribute.setLeftSecondarySpacing(spacingSec);
        attribute.setSildeLineColor(bLineC);
        attribute.setSildeLineHeight(bLineH);
        attribute.setTopLeftTextColor(topTextC);
        attribute.setTopLeftTextSize(topTextS);
        attribute.setMinCentreIconWidth(cenPicW);
        attribute.setMinCentreIconHeight(cenPicH);
        attribute.setMinBottomIconWidth(bPicW);
        attribute.setMinBottomIconHeight(bPicH);
        SildeBarConfigBuilder builder = new SildeBarConfigBuilder();
        builder.setContent(model)
                .setAttribute(attribute)
                .build();
        perform();
    }

    /**
     * 开始画图。
     */
    public void perform() {
        isShow = true;
        invalidate();
    }

    private void startDraw() {
        mLeftMoveX = mMinWidth / 2;
        mRightMoveX = getWidth() - mMaxWidth / 2;
        mMinText = mSildeBarConfigBuilder.getContent().getMinValue()
                + mSildeBarConfigBuilder.getContent().getAdditionalText();
        mMaxText = mSildeBarConfigBuilder.getContent().getMaxValue()
                + mSildeBarConfigBuilder.getContent().getAdditionalRightText();
        mCurrentMin = mSildeBarConfigBuilder.getContent().getMinValue();
        mCurrentMax = mSildeBarConfigBuilder.getContent().getMaxValue();
        mLeftRange = new Rect(0, 0, mMinWidth, getHeight());
        mRightRange = new Rect(getWidth() - mMaxWidth, 0, getWidth(), getHeight());
    }

    /**
     * 获取当前选中的最小值。
     *
     * @return
     */
    public int getMin() {
        return mCurrentMin;
    }

    /**
     * 获取当前选中的最大值。
     *
     * @return
     */
    public int getMax() {
        return mCurrentMax;
    }

    public void setTouchObserver(SildeTouchUpObserver sildeTouchUpObserver) {
        mSildeTouchUpObserver = sildeTouchUpObserver;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mTouchX = (int) event.getX();
        mTouchY = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchDownX = mTouchX;
                mTouchDownY = mTouchY;
                Rect rect = new Rect(mLeftRange);
                rect.left -= SILDEOFFSET;
                rect.right += SILDEOFFSET;
                Rect rect1 = new Rect(mRightRange);
                rect1.left -= SILDEOFFSET;
                rect1.right += SILDEOFFSET;
                if (rect.contains(mTouchDownX, mTouchY)) {
                    orginal = orginal | SELECTMIN;
                    // isRightSlide = true;
                }

                if (rect1.contains(mTouchDownX, mTouchY)) {
                    orginal = orginal | SELECTMAX;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (orginal == SELECTAll) {
                    if (mTouchX - mTouchDownX >= 0) {
                        orginal = orginal & SELECTMAX;
                    } else {
                        orginal = orginal & SELECTMIN;
                    }
                }
                if (orginal == SELECTMIN) {
                    mLeftMoveX = mTouchX;
//                    mLeftMoveX = Math.max(mMinWidth / 2, Math.min(mRightRange.centerX(), mLeftMoveX - mMinWidth / 2));
                    mLeftRange.left = mLeftMoveX - mMinWidth / 2;
                    mLeftRange.right = mLeftMoveX + mMinWidth / 2;
                    int max = mSildeBarConfigBuilder.getContent().getMaxValue();
                    int min = mSildeBarConfigBuilder.getContent().getMinValue();
                    float h = getWidth() - Math.max(mMinWidth, mMaxWidth); // 滑动线总长度
//                    float scale = ((mLeftMoveX - Math.max(mMinWidth, mMaxWidth) / 2) * (max - min)) / h;
                    float scale = ((mLeftMoveX - mMinWidth / 2) * (max - min)) / h;
                    mMinText = Math.round(Math.max(scale, 0) + min) + mSildeBarConfigBuilder.getContent().getAdditionalText();
//                    mCurrentMin = Math.round(Math.max(scale, 0) + min);
                }
                if (orginal == SELECTMAX) {
                    mRightMoveX = mTouchX;
                    mRightMoveX = Math.max(mLeftRange.centerX(), Math.min(getWidth() - mMaxWidth / 2, mRightMoveX - mMaxWidth / 2));
                    mRightRange.left = mRightMoveX - mMaxWidth / 2;
                    mRightRange.right = mRightMoveX + mMaxWidth / 2;
                    int max = mSildeBarConfigBuilder.getContent().getMaxValue();
                    int min = mSildeBarConfigBuilder.getContent().getMinValue();
                    float h = getWidth() - Math.max(mMinWidth, mMaxWidth);
                    float scale = ((mRightMoveX - Math.max(mMinWidth, mMaxWidth) / 2) * (max - min)) / h;
                    mMaxText = Math.round(min + Math.max(scale, 0)) + mSildeBarConfigBuilder.getContent().getAdditionalRightText();
                    mCurrentMax = Math.round(min + Math.max(scale, 0));
                }
                drawVarious(null);
                if (mMinText.equals(mMaxText)) {
                    mLeftRange = mRightRange;
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                orginal = orginal & 4;
                if (TextUtils.equals(mMinText, mMaxText)) {
                    mLeftRange = new Rect(mRightRange);
                    mLeftMoveX = mRightMoveX;
                    invalidate();
                }
                if (mSildeTouchUpObserver != null) {
                    mSildeTouchUpObserver.sildeTouchUp();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                if (TextUtils.equals(mMinText, mMaxText)) {
                    mLeftRange = new Rect(mRightRange);
                    mLeftMoveX = mRightMoveX;
                    invalidate();
                }
                if (orginal == 0) {
                    break;
                }
                orginal = orginal & 4;
                if (mSildeTouchUpObserver != null) {
                    mSildeTouchUpObserver.sildeTouchUp();
                }
//                if (mMinText.equals(mMaxText)){
//                    mLeftRange = mRightRange;
//                    invalidate();
//                }
                break;
        }
        return true;
    }

    /**
     * 计算右边的视图的最大宽度。
     */
    private void getRightMaxWidth() {
        String min = mSildeBarConfigBuilder.getContent().getMaxValue()
                + mSildeBarConfigBuilder.getContent().getAdditionalRightText();
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(mSildeBarConfigBuilder.getAttribute().getTopRightTextSize());
        paint.setColor(mSildeBarConfigBuilder.getAttribute().getTopRightTextColor());
        mMaxTextHeight = Math.round(Math.abs(paint.getFontMetrics().ascent) + Math.abs(paint.getFontMetrics().descent));
        Rect rect = new Rect();
        paint.getTextBounds(min, 0, min.length(), rect);
        int mMaxTextWidth = getTextWidth(paint, min);
        // int mMaxTextWidth = paint.getTextWidths(min, 0, min.length(),new float[min.length()]);
        int maxWidth = Math.max(mSildeBarConfigBuilder.getAttribute().getMaxCentreIconWidth(),
                Math.max(mMaxTextWidth, mSildeBarConfigBuilder.getAttribute().getMaxBottomIconWidth()));
        mMaxWidth = maxWidth;
    }

    /**
     * 计算左边的视图的最大宽度。
     */
    private void getLeftMaxWidth() {
        String min = mSildeBarConfigBuilder.getContent().getMinValue()
                + mSildeBarConfigBuilder.getContent().getAdditionalText();
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(mSildeBarConfigBuilder.getAttribute().getTopLeftTextSize());
        paint.setColor(mSildeBarConfigBuilder.getAttribute().getTopLeftTextColor());
        mMinTextHeight = Math.round(Math.abs(paint.getFontMetrics().ascent) + Math.abs(paint.getFontMetrics().descent));
        Rect rect = new Rect();
        paint.getTextBounds(min, 0, min.length(), rect);
        int mMinTextWidth = rect.width();
        int maxWidth = Math.max(mSildeBarConfigBuilder.getAttribute().getMinCentreIconWidth(),
                Math.max(mMinTextWidth, mSildeBarConfigBuilder.getAttribute().getMinBottomIconWidth()));
        mMinWidth = maxWidth;
    }

    /**
     * 话各种图。
     */
    private void drawVarious(Rect drawRect) {
        //canvas = lockCanvas(drawRect);
        if (canvas == null) {
            return;
        }
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        canvas.drawPaint(paint);
        canvas.drawColor(Color.WHITE);
        drawTopLeftText();
        drawBottomLine();
        drawLeftPic();
        drawRighttPic();
        drawTopRightText();
        //unlockCanvasAndPost(canvas);
    }

    /**
     * 画左边图片。
     */
    private void drawLeftPic() {
        int cId = mSildeBarConfigBuilder.getContent().getMinCentreIconResourceId();
        int cw = mSildeBarConfigBuilder.getAttribute().getMinCentreIconWidth();
        int ch = mSildeBarConfigBuilder.getAttribute().getMinCentreIconHeight();
        int cy = mMinTextHeight + mSildeBarConfigBuilder.getAttribute().getLeftSpacing();
        drawCentreLeftIcon(canvas, cId, cw, ch, mLeftMoveX, cy);
        int bId = mSildeBarConfigBuilder.getContent().getMinBottomIconResourceId();
        int bw = mSildeBarConfigBuilder.getAttribute().getMinBottomIconWidth();
        int bh = mSildeBarConfigBuilder.getAttribute().getMinBottomIconHeight();
        int by = cy + ch + mSildeBarConfigBuilder.getAttribute().getLeftSecondarySpacing();
        drawBottomLeftIcon(canvas, bId, bw, bh, mLeftMoveX, by);
    }

    /**
     * 画右边图片。
     */
    private void drawRighttPic() {
        int cId = mSildeBarConfigBuilder.getContent().getMaxCentreIconResourceId();
        int cw = mSildeBarConfigBuilder.getAttribute().getMaxCentreIconWidth();
        int ch = mSildeBarConfigBuilder.getAttribute().getMaxCentreIconHeight();
        int cy = mMaxTextHeight + mSildeBarConfigBuilder.getAttribute().getRightSpacing();

        int bId = mSildeBarConfigBuilder.getContent().getMaxBottomIconResourceId();
        int bw = mSildeBarConfigBuilder.getAttribute().getMaxBottomIconWidth();
        int bh = mSildeBarConfigBuilder.getAttribute().getMaxBottomIconHeight();
        int by = cy + ch + mSildeBarConfigBuilder.getAttribute().getRightSecondarySpacing();
        drawBottomRightIcon(canvas, bId, bw, bh, mRightMoveX, by);
        drawCentreRightIcon(canvas, cId, cw, ch, mRightMoveX, by + bh + mSildeBarConfigBuilder.getAttribute().getRightSecondarySpacing());

    }

    /**
     * 画底部的滑动线。
     */
    private void drawBottomLine() {
        int chl = mSildeBarConfigBuilder.getAttribute().getMinCentreIconHeight();
        int cyl = mMinTextHeight + mSildeBarConfigBuilder.getAttribute().getLeftSpacing();
        int bhl = mSildeBarConfigBuilder.getAttribute().getMinBottomIconHeight();
        int byl = cyl + chl + mSildeBarConfigBuilder.getAttribute().getLeftSecondarySpacing();

        int chr = mSildeBarConfigBuilder.getAttribute().getMaxCentreIconHeight();
        int cyr = mMaxTextHeight + mSildeBarConfigBuilder.getAttribute().getRightSpacing();
        int bhr = mSildeBarConfigBuilder.getAttribute().getMaxBottomIconHeight();
        int byr = cyr + chr + mSildeBarConfigBuilder.getAttribute().getRightSecondarySpacing();

        float xl = mMinWidth / 2;
        int[] form = new int[]{Math.round(xl), byl + bhl / 2}; // 滑动线最左端
        int[] to = new int[]{getWidth() - mMaxWidth / 2, byr + bhr / 2}; // 滑动线最右端
        int color = mSildeBarConfigBuilder.getAttribute().getSildeLineColor();
        int height = mSildeBarConfigBuilder.getAttribute().getSildeLineHeight();
        drawBottomLine(canvas, color, form, to, height);
    }

    /**
     * view销毁后的操作。
     */
    private void expire() {
        mCheckTag = false;
        if (mLeftCentreIcon != null) {
            mLeftCentreIcon.recycle();
            mLeftCentreIcon = null;
        }
        if (mRightCenreIcon != null) {
            mRightCenreIcon.recycle();
            mRightCenreIcon = null;
        }
        if (mBottomLeftIcon != null) {
            mBottomLeftIcon.recycle();
            mBottomLeftIcon = null;
        }
        if (mBottomRightIcon != null) {
            mBottomRightIcon.recycle();
            mBottomRightIcon = null;
        }
    }

    /**
     * 画左上角的文字。
     */
    private void drawTopLeftText() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(mSildeBarConfigBuilder.getAttribute().getTopLeftTextSize());
        paint.setColor(mSildeBarConfigBuilder.getAttribute().getTopLeftTextColor());
        mMinTextHeight = Math.round(Math.abs(paint.getFontMetrics().ascent) + Math.abs(paint.getFontMetrics().descent));
        Rect rect = new Rect();
        paint.getTextBounds(mMinText, 0, mMinText.length(), rect);
        int mMinTextWidth = rect.width();
        int mMinTextY = 0 + Math.abs(Math.round(paint.getFontMetrics().ascent));
        int starX = (int) (mLeftMoveX - mMinTextWidth / 2f);
        canvas.drawText(mMinText, starX, mMinTextY, paint);
    }

    /**
     * 画右上角的文字。
     */
    private void drawTopRightText() {
        int ch = mSildeBarConfigBuilder.getAttribute().getMaxCentreIconHeight();
        int cy = mMaxTextHeight + mSildeBarConfigBuilder.getAttribute().getRightSpacing();

        int bh = mSildeBarConfigBuilder.getAttribute().getMaxBottomIconHeight();
        int by = cy + ch + mSildeBarConfigBuilder.getAttribute().getRightSecondarySpacing();

        int y = by + bh + mSildeBarConfigBuilder.getAttribute().getRightSecondarySpacing() + ch +
                mSildeBarConfigBuilder.getAttribute().getRightSpacing();
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(mSildeBarConfigBuilder.getAttribute().getTopRightTextSize());
        paint.setColor(mSildeBarConfigBuilder.getAttribute().getTopRightTextColor());
        mMaxTextHeight = Math.round(Math.abs(paint.getFontMetrics().ascent) + Math.abs(paint.getFontMetrics().descent));
//        Rect rect = new Rect();
//        paint.getTextBounds(mMaxText, 0, mMaxText.length(), rect);
//        int mMaxTextWidth = rect.width();
        int mMaxTextY = y + Math.abs(Math.round(paint.getFontMetrics().ascent));
        int starX = mRightMoveX - mMaxWidth / 2;
        canvas.drawText(mMaxText, starX, mMaxTextY, paint);
    }

    /**
     * 画左边中间的图片。
     *
     * @param canvas
     * @param rId
     * @param width
     * @param height
     * @param x
     * @param y
     */
    private void drawCentreLeftIcon(Canvas canvas, int rId, int width, int height, int x, int y) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        if (mLeftCentreIcon == null) {
            BitmapFactory.Options optians = new BitmapFactory.Options();
//            optians.inJustDecodeBounds = false;
//            optians.outWidth = width;
//            optians.outHeight = height;
            mLeftCentreIcon = BitmapFactory.decodeResource(getResources(), rId, optians);
        }
        RectF rectF = new RectF();
        rectF.left = x - width / 2;
        rectF.top = y;
        rectF.right = rectF.left + width;
        rectF.bottom = y + height;
        canvas.drawBitmap(mLeftCentreIcon, null, rectF, paint);
    }

    /**
     * 画右边中间的图片。
     *
     * @param canvas
     * @param rId
     * @param width
     * @param height
     * @param x
     * @param y
     */
    private void drawCentreRightIcon(Canvas canvas, int rId, int width, int height, int x, int y) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        if (mRightCenreIcon == null) {
            BitmapFactory.Options optians = new BitmapFactory.Options();
//            optians.inJustDecodeBounds = false;
//            optians.outWidth = width;
//            optians.outHeight = height;
            mRightCenreIcon = BitmapFactory.decodeResource(getResources(), rId, optians);
        }
        int maxWidth = mMaxWidth;
        float sx = x - width / 2;
        RectF rectF = new RectF(sx, y, sx + width, y + height);
        canvas.drawBitmap(mRightCenreIcon, null, rectF, paint);
    }

    /**
     * 画左边底部的icon。
     *
     * @param canvas
     * @param rId
     * @param width
     * @param height
     * @param x
     * @param y
     */
    private void drawBottomLeftIcon(Canvas canvas, int rId, int width, int height, int x, int y) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        if (mBottomLeftIcon == null) {
            BitmapFactory.Options optians = new BitmapFactory.Options();
//            optians.inJustDecodeBounds = false;
//            optians.outWidth = width;
//            optians.outHeight = height;
            mBottomLeftIcon = BitmapFactory.decodeResource(getResources(), rId, optians);
        }
        float sx = x - width / 2;
        RectF rectF = new RectF(sx, y, sx + width, y + height);
        canvas.drawBitmap(mBottomLeftIcon, null, rectF, paint);
    }

    /**
     * 画右边底部的icon。
     *
     * @param canvas
     * @param rId
     * @param width
     * @param height
     * @param x
     * @param y
     */
    private void drawBottomRightIcon(Canvas canvas, int rId, int width, int height, int x, int y) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        if (mBottomRightIcon == null) {
            BitmapFactory.Options optians = new BitmapFactory.Options();
//            optians.inJustDecodeBounds = false;
//            optians.outWidth = width;
//            optians.outHeight = height;
            mBottomRightIcon = BitmapFactory.decodeResource(getResources(), rId, optians);
        }
        float sx = x - width / 2;
        RectF rectF = new RectF(sx, y, sx + width, y + height);
        canvas.drawBitmap(mBottomRightIcon, null, rectF, paint);
    }

    /**
     * 画底部的滑动线。
     *
     * @param canvas
     * @param color
     * @param from
     * @param to
     * @param height
     */
    private void drawBottomLine(Canvas canvas, int color, int[] from, int[] to, int height) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStrokeWidth(height);
        canvas.drawLine(from[0], from[1], to[0], from[1], paint);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        if (isSurface) {
            drawVarious(null);
        }
        isSurface = true;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        expire();
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

    public static int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }

    public class SildeBarConfigBuilder {

        private SildeBarContentModel content;
        private SildeBarAttributeModel attribute;

        public SildeBarContentModel getContent() {
            return content;
        }

        public SildeBarConfigBuilder setContent(SildeBarContentModel content) {
            this.content = content;
            return this;
        }

        public SildeBarAttributeModel getAttribute() {
            return attribute;
        }

        public SildeBarConfigBuilder setAttribute(SildeBarAttributeModel attribute) {
            this.attribute = attribute;
            return this;
        }

        public void build() {
            mSildeBarConfigBuilder = this;
        }

    }

    public class SildeBarAttributeModel {
        private int topLeftTextSize, topRightTextSize;//顶部左右显示文本的字号。
        private int topLeftTextColor, topRightTextColor;//顶部左右显示文本的颜色。
        private int sildeLineColor;//滑动辅助线的颜色。
        private int sildeLineHeight;//滑动辅助线的高度。
        private int minCentreIconWidth, minCentreIconHeight;//最小值中间滑动的图片的宽高。
        private int maxCentreIconWidth, maxCentreIconHeight;//最大值中间滑动的图片的宽高。
        private int minBottomIconWidth, minBottomIconHeight;//最小值底部滑动的图片的宽高。
        private int maxBottomIconWidth, maxBottomIconHeight;//最大值底部滑动的图片的宽高。
        private int leftSpacing;//左边第一个间距。
        private int leftSecondarySpacing;//左边第二个间距。
        private int rightSpacing;//右边第一个间距。
        private int rightSecondarySpacing;//右边第二个间距。

        public int getLeftSpacing() {
            return leftSpacing;
        }

        public SildeBarAttributeModel setLeftSpacing(int leftSpacing) {
            this.leftSpacing = leftSpacing;
            setRightSpacing(leftSpacing);
            return this;
        }

        public int getLeftSecondarySpacing() {
            return leftSecondarySpacing;
        }

        public SildeBarAttributeModel setLeftSecondarySpacing(int leftSecondarySpacing) {
            this.leftSecondarySpacing = leftSecondarySpacing;
            setRightSecondarySpacing(leftSecondarySpacing);
            return this;
        }

        public int getRightSpacing() {
            return rightSpacing;
        }

        public SildeBarAttributeModel setRightSpacing(int rightSpacing) {
            this.rightSpacing = rightSpacing;
            return this;
        }

        public int getRightSecondarySpacing() {
            return rightSecondarySpacing;
        }

        public SildeBarAttributeModel setRightSecondarySpacing(int rightSecondarySpacing) {
            this.rightSecondarySpacing = rightSecondarySpacing;
            return this;
        }

        public int getTopLeftTextSize() {
            return topLeftTextSize;
        }

        public SildeBarAttributeModel setTopLeftTextSize(int topLeftTextSize) {
            this.topLeftTextSize = topLeftTextSize;
            setTopRightTextSize(topLeftTextSize);
            return this;
        }

        public int getTopRightTextSize() {
            return topRightTextSize;
        }

        public SildeBarAttributeModel setTopRightTextSize(int topRightTextSize) {
            this.topRightTextSize = topRightTextSize;
            return this;
        }

        public int getTopLeftTextColor() {
            return topLeftTextColor;
        }

        public SildeBarAttributeModel setTopLeftTextColor(int topLeftTextColor) {
            this.topLeftTextColor = topLeftTextColor;
            setTopRightTextColor(topLeftTextColor);
            return this;
        }

        public int getTopRightTextColor() {
            return topRightTextColor;
        }

        public SildeBarAttributeModel setTopRightTextColor(int topRightTextColor) {
            this.topRightTextColor = topRightTextColor;
            return this;
        }

        public int getSildeLineColor() {
            return sildeLineColor;
        }

        public SildeBarAttributeModel setSildeLineColor(int sildeLineColor) {
            this.sildeLineColor = sildeLineColor;
            return this;
        }

        public int getSildeLineHeight() {
            return sildeLineHeight;
        }

        public SildeBarAttributeModel setSildeLineHeight(int sildeLineHeight) {
            this.sildeLineHeight = sildeLineHeight;
            return this;
        }

        public int getMinCentreIconWidth() {
            return minCentreIconWidth;
        }

        public SildeBarAttributeModel setMinCentreIconWidth(int minCentreIconWidth) {
            this.minCentreIconWidth = minCentreIconWidth;
            setMaxCentreIconWidth(minCentreIconWidth);
            return this;
        }

        public int getMinCentreIconHeight() {
            return minCentreIconHeight;
        }

        public SildeBarAttributeModel setMinCentreIconHeight(int minCentreIconHeight) {
            this.minCentreIconHeight = minCentreIconHeight;
            setMaxCentreIconHeight(minCentreIconHeight);
            return this;
        }

        public int getMaxCentreIconWidth() {
            return maxCentreIconWidth;
        }

        public SildeBarAttributeModel setMaxCentreIconWidth(int maxCentreIconWidth) {
            this.maxCentreIconWidth = maxCentreIconWidth;
            return this;
        }

        public int getMaxCentreIconHeight() {
            return maxCentreIconHeight;
        }

        public SildeBarAttributeModel setMaxCentreIconHeight(int maxCentreIconHeight) {
            this.maxCentreIconHeight = maxCentreIconHeight;
            return this;
        }

        public int getMinBottomIconWidth() {
            return minBottomIconWidth;
        }

        public SildeBarAttributeModel setMinBottomIconWidth(int minBottomIconWidth) {
            this.minBottomIconWidth = minBottomIconWidth;
            setMaxBottomIconWidth(minBottomIconWidth);
            return this;
        }

        public int getMinBottomIconHeight() {
            return minBottomIconHeight;
        }

        public SildeBarAttributeModel setMinBottomIconHeight(int minBottomIconHeight) {
            this.minBottomIconHeight = minBottomIconHeight;
            setMaxBottomIconHeight(minBottomIconHeight);
            return this;
        }

        public int getMaxBottomIconWidth() {
            return maxBottomIconWidth;
        }

        public SildeBarAttributeModel setMaxBottomIconWidth(int maxBottomIconWidth) {
            this.maxBottomIconWidth = maxBottomIconWidth;
            return this;
        }

        public int getMaxBottomIconHeight() {
            return maxBottomIconHeight;
        }

        public SildeBarAttributeModel setMaxBottomIconHeight(int maxBottomIconHeight) {
            this.maxBottomIconHeight = maxBottomIconHeight;
            return this;
        }


    }

    public class SildeBarContentModel {
        private int minValue, maxValue;//滑动最小值和最大值。
        private String additionalText = "";
        private String additionalRightText = "";//附加文本。
        private int minCentreIconResourceId, maxCentreIconResourceId;//最小值中间滑动的图片id，最大值中间滑动的图片id;
        private int minBottomIconResourceId, maxBottomIconResourceId;//最小值底部滑动的图片id，最大值底部滑动的图片id;

        public int getMinValue() {
            return minValue;
        }

        public SildeBarContentModel setMinValue(int minValue) {
            this.minValue = minValue;
            return this;
        }

        public int getMaxValue() {
            return maxValue;
        }


        public String getAdditionalRightText() {
            return additionalRightText;
        }

        public SildeBarContentModel setAdditionalRightText(String additionalRightText) {
            this.additionalRightText = additionalRightText;
            return this;
        }

        public SildeBarContentModel setMaxValue(int maxValue) {
            this.maxValue = maxValue;
            return this;
        }

        public String getAdditionalText() {
            return additionalText;
        }

        public SildeBarContentModel setAdditionalText(String additionalText) {
            this.additionalText = additionalText;
            return this;
        }

        public int getMinCentreIconResourceId() {
            return minCentreIconResourceId;
        }

        public SildeBarContentModel setMinCentreIconResourceId(int minCentreIconResourceId) {
            this.minCentreIconResourceId = minCentreIconResourceId;
            setMaxCentreIconResourceId(minCentreIconResourceId);
            return this;
        }

        public int getMaxCentreIconResourceId() {
            return maxCentreIconResourceId;
        }

        public SildeBarContentModel setMaxCentreIconResourceId(int maxCentreIconResourceId) {
            this.maxCentreIconResourceId = maxCentreIconResourceId;
            return this;
        }

        public int getMinBottomIconResourceId() {
            return minBottomIconResourceId;
        }

        public SildeBarContentModel setMinBottomIconResourceId(int minBottomIconResourceId) {
            this.minBottomIconResourceId = minBottomIconResourceId;
            setMaxBottomIconResourceId(minBottomIconResourceId);
            return this;
        }

        public int getMaxBottomIconResourceId() {
            return maxBottomIconResourceId;
        }

        public SildeBarContentModel setMaxBottomIconResourceId(int maxBottomIconResourceId) {
            this.maxBottomIconResourceId = maxBottomIconResourceId;
            return this;
        }
    }

    public static abstract class SildeTouchUpObserver {
        public abstract void sildeTouchUp();
    }

}
